#ifdef FSHIGHPRECISION
precision highp float;
#else
precision mediump float;
#endif

uniform vec4 u_Albedo;

#ifdef ALPHATEST
uniform float u_AlphaTestValue;
#endif

#ifdef DIFFUSEMAP
uniform sampler2D u_DiffuseTexture;
#endif

#ifdef REFLECTMAP
uniform samplerCube u_ReflectTexture;
uniform vec3 u_MaterialReflect;
#endif


#ifdef DIFFUSEMAP||((DIRECTIONLIGHT||POINTLIGHT||SPOTLIGHT)&&COLOR&&SPECULARMAP)
varying vec2 v_Texcoord0;
#endif

#ifdef AMBIENTMAP
varying vec2 v_Texcoord1;
uniform sampler2D u_AmbientTexture;
#endif

#ifdef DIRECTIONLIGHT||POINTLIGHT||SPOTLIGHT||AMBIENTMAP
uniform vec3 u_MaterialAmbient;
#endif

#ifdef COLOR
varying vec4 v_Color;
#endif

#ifdef DIRECTIONLIGHT||POINTLIGHT||SPOTLIGHT
varying vec3 v_Diffuse;
varying vec3 v_Ambient;
varying vec3 v_Specular;
  #ifdef (DIFFUSEMAP||COLOR)&&SPECULARMAP
  uniform sampler2D u_SpecularTexture;
  #endif
#endif

#ifdef FOG
uniform float u_FogStart;
uniform float u_FogRange;
uniform vec3 u_FogColor;
varying float v_ToEyeLength;
#endif

#ifdef MIXUV
uniform float  u_UVAniAge;
#endif

#ifdef REFLECTMAP
varying vec3 v_Normal;
varying vec3 v_ToEye;
#endif


void main()
{
 #ifdef DIFFUSEMAP&&!COLOR
 gl_FragColor=texture2D(u_DiffuseTexture, v_Texcoord0);
 #endif 
 
 #ifdef COLOR&&!DIFFUSEMAP
 gl_FragColor=v_Color;
 #endif 
 
 #ifdef DIFFUSEMAP&&COLOR
 vec4 texColor=texture2D(u_DiffuseTexture, v_Texcoord0);
 gl_FragColor=texColor*v_Color;
 #endif
 
 #ifdef !DIFFUSEMAP&&!COLOR
 gl_FragColor=vec4(1.0,1.0,1.0,1.0);
 #endif 
 
 #ifdef AMBIENTMAP
 gl_FragColor.rgb=gl_FragColor.rgb*(u_MaterialAmbient+texture2D(u_AmbientTexture, v_Texcoord1).rgb);
 #endif 
 
 gl_FragColor=gl_FragColor*u_Albedo;
  
 #ifdef ALPHATEST
   if(gl_FragColor.a-u_AlphaTestValue<0.0)
    discard;
 #endif
 
 
 #ifdef REFLECTMAP
 vec3 normal=normalize(v_Normal);
 #endif 	

  
 #ifdef DIRECTIONLIGHT||POINTLIGHT||SPOTLIGHT
   #ifdef (DIFFUSEMAP||COLOR)&&SPECULARMAP
   vec3 specular =v_Specular*texture2D(u_SpecularTexture,v_Texcoord0).rgb;
   gl_FragColor =vec4( gl_FragColor.rgb*(v_Ambient + v_Diffuse)+specular,gl_FragColor.a);
   #else
   gl_FragColor =vec4( gl_FragColor.rgb*(v_Ambient + v_Diffuse)+v_Specular,gl_FragColor.a);
   #endif
 #endif
 
 #ifdef REFLECTMAP
 vec3 incident = -v_ToEye;
 vec3 reflectionVector = reflect(incident,v_Normal);
 vec3 reflectionColor  = textureCube(u_ReflectTexture,reflectionVector).rgb;
 gl_FragColor.rgb += u_MaterialReflect*reflectionColor;
 #endif
 
 #ifdef FOG
 float lerpFact=clamp((v_ToEyeLength-u_FogStart)/u_FogRange,0.0,1.0);
 gl_FragColor.rgb=mix(gl_FragColor.rgb,u_FogColor,lerpFact);
 #endif
}

